/**
 * 学生报名实习岗位
 * 
 * 校内岗位报名人数不能超出可报名人数
 * 校外岗位没有限制
 * 
 * 
 * /student/chooisejob
 */

 
const Companies = require('../../../models/companys')
const Students = require('../../../models/internshipStudents')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')
const MySQLManager = require('../../../utils/MySQLManager');
const sequelize = MySQLManager


const chooise_job = async function(req, res) {    
    try {
        if (!req.body.company_id) {
            $.sendMessage(res, 1015, '参数company_id不存在')
            return
        }

        // 查看学生是否被锁定
        const is_locked = await Students.findOne({
            where: {
                student_user_name: req.user_name
            }
        })
        if (is_locked == null) {
            $.sendMessage(res, 1015, '学生不存在id')
            return
        } else if (is_locked.dataValues.is_locked == 0) {
            $.sendMessage(res, 1015, '您被锁定，不能报名')
            return
        }

        // 先看看该岗位是否存在，是否校内岗位，是否已满,还要判断学生和岗位是否在同一个年度里
        const job_info = await Companies.findOne({
            where: {
                id: req.body.company_id
            }
        })
        if (job_info == null) {
            $.sendMessage(res, 1015, '无效id')
            return
        }
        
        // 找到学生查看是否与该岗位属于同一个年度
        const student = await Students.findOne({
            where: {
                student_year: job_info.dataValues.company_year,
                student_user_name: req.user_name
            }
        })
        if (student == null) {
            $.sendMessage(res, 1015, '你不在该年度里，请联系老师')
            return
        }
        if (student.dataValues.company_id != '') {
            $.sendMessage(res, 1015, '您已报名别的岗位，请在未报名状态下进行报名')
            return
        }
    
        // 校内岗位,需要限制报名人数
        if (job_info.dataValues.type == '0') {
            if (parseInt(job_info.dataValues.student_limit) > parseInt(job_info.dataValues.current_student)) {
                // 报名，修改学生报名信息，修改岗位报名人数
                // 事务
                return sequelize.transaction(function (t) {

                    return Students.update(
                    {
                        student_status: '1',
                        company_name: job_info.dataValues.company_name,
                        company_id: job_info.dataValues.id,
                        teacher_user_fullname: job_info.dataValues.recommend_teacher,
                    },
                    {
                        where: {
                            student_user_name: req.user_name
                        }
                    }
                    , 
                    {transaction: t}).then(function (user) {
                    return Companies.findOne(
                        {
                            where: {
                                id: req.body.company_id
                            }
                        }
                        , {transaction: t})
                    }).then( company => {

                    return company.increment('current_student'
                    ,{transaction: t}).then( () => {
                    })
                    })
                })
                // 请求完整完成
                .then(function (result) {
                    res.json({
                        code: 200,
                        message: '报名成功',
                        result: result
                    })
                    utils.create_a_log(req, '学生实习', '报名校内实习')
                    return
                })
                // 处理sequalize异常
                .catch(function (err) {
                    res.json({
                        code: 1017,
                        message: '报名失败，请联系管理员',
                        err: 'err' + err
                    })
                    return
                })

            }
            else {
                $.sendMessage(res, 1015, '该岗位报名人数已满，请报名其它岗位')
                return
            }
        }

        // 校外岗位不需要限制人数
        else if (job_info.dataValues.type == '1') {
            // 报名，修改学生报名信息，修改岗位报名人数
                // 事务
                return sequelize.transaction(function (t) {

                    return Students.update(
                    {
                        student_status: '1',
                        company_name: job_info.dataValues.company_name,
                        company_id: job_info.dataValues.id,
                        teacher_user_fullname: job_info.dataValues.recommend_teacher,
                    },
                    {
                        where: {
                            student_user_name: req.user_name
                        }
                    }
                    , 
                    {transaction: t}).then(function (user) {
                    return Companies.findOne(
                        {
                            where: {
                                id: req.body.company_id
                            }
                        }
                        , {transaction: t})
                    }).then( company => {

                    return company.increment('current_student'
                    ,{transaction: t}).then( () => {
                    })
                    })
                })
                // 请求完整完成
                .then(function (result) {
                    res.json({
                        code: 200,
                        message: '报名成功',
                        result: result
                    })
                    utils.create_a_log(req, '学生实习', '报名校外实习')
                    return
                })
                // 处理sequalize异常
                .catch(function (err) {
                    res.json({
                        code: 1017,
                        message: '报名失败，请联系管理员',
                        err: 'err' + err
                    })
                    return
                })
        }
    }

    catch(err) {
        $.sendMessage(res, 0000, "err:" + err)
        utils.create_a_log(req, '学生入所', '报名题目', '失败')
        return
    }
}

module.exports = chooise_job